package Problem5;

public class MyLinkedList<E> {

    private int size = 0;
    private NodeCode hand;
    class NodeCode{
        E data;
        NodeCode nextNodeCode;

        public  NodeCode(E data, NodeCode nextNodeCode) {
            this.data = data;
            this.nextNodeCode = nextNodeCode;
        }
    }
    public NodeCode add(E e){
            if (hand == null){
                hand = new NodeCode(e,null);
            }else {
                NodeCode temp = hand;
                while (temp.nextNodeCode != null){
                    temp = temp.nextNodeCode;
                }
                temp.nextNodeCode = new NodeCode(e,null);
            }
        size++;
        return hand;
    }
    public void forEach(MyConsumer<E> myConsumer){
        NodeCode first = hand;
        while ( first!=null ){
            myConsumer.accept(first.data);
            first = first.nextNodeCode;
        }
    }
    public void reverse(int left,int right){
        NodeCode first = hand;
        int count = 1;
        NodeCode tempLeft = null;
        Object[] arr = new Object[right-left + 1];
        while (count <= right){
            if (count == left){
                tempLeft = first;
            }
            if (count >= left ) {
                arr[count-left] = first.data;
            }
            first = first.nextNodeCode;
            count++;
           /* if (count == right){
                arr[count-left] = first.data;
            }*/
        }
        for (int i = arr.length - 1; i >= 0; i--) {
            tempLeft.data = (E) arr[i];
            tempLeft = tempLeft.nextNodeCode;
        }
    }
}
